<template>
  <div>
    <el-row :gutter="10">
      <el-col :xs="8" :sm="6" :md="4" :lg="3" :xl="1"><div class="grid-content bg-purple"></div></el-col>
      <el-col :xs="4" :sm="6" :md="8" :lg="9" :xl="11"><div class="grid-content bg-purple-light"></div></el-col>
      <el-col :xs="4" :sm="6" :md="8" :lg="9" :xl="11"><div class="grid-content bg-purple"></div></el-col>
      <el-col :xs="8" :sm="6" :md="4" :lg="3" :xl="1"><div class="grid-content bg-purple-light"></div></el-col>
    </el-row>
     <div class='sub-sort'>
        <div class="sort">
          <a href="javascript:;" :class="{active:!sortParams.sortField}" @click="changeSort(null)">默认排序</a>
          <a href="javascript:;" :class="{active:sortParams.sortField==='publishTime'}" @click="changeSort('publishTime')">最新商品</a>
          <a href="javascript:;" :class="{active:sortParams.sortField==='orderNum'}" @click="changeSort('orderNum')">最高人气</a>
          <a href="javascript:;" :class="{active:sortParams.sortField==='evaluateNum'}" @click="changeSort('evaluateNum')">评论最多</a>
          <a href="javascript:;"  @click="changeSort('price')">
            价格排序
            <i class="arrow up" :class="{active:sortParams.sortField==='price'&&sortParams.sortMethod==='asc' }" />
            <i class="arrow down" :class="{active:sortParams.sortField==='price'&&sortParams.sortMethod==='desc'}" />
          </a>
        </div>
        <div class="check">
         <el-checkbox @change="changeCheck" v-model="sortParams.inventory">仅显示有货商品</el-checkbox>
         <el-checkbox @change="changeCheck" v-model="sortParams.onlyDiscount">仅显示特惠商品</el-checkbox>
        </div>
      </div>
  </div>
</template>

<script>
import { reactive } from '@vue/reactivity'

export default {
  setup (props, { emit }) {
    const sortParams = reactive({
      inventory: false,
      onlyDiscount: false,
      sortField: null,
      sortMethod: null
    })
    // 绑定点击事件
    const changeSort = (sortField) => {
      if (sortField === 'price') {
        sortParams.sortField = sortField
        // 处理排序 没有值表示第一次点击价格排序
        if (!sortParams.sortMethod) {
          sortParams.sortMethod = 'desc'
        } else {
          sortParams.sortMethod = sortParams.sortMethod === 'desc' ? 'asc' : 'desc'
          // if (sortParams.sortMethod === 'desc') {
          //   sortParams.sortMethod = 'asc'
          //   return ''
          // }
          // sortParams.sortMethod = 'desc'
        }
      } else {
        // 如果已经点击了 再次点击会在发请求所以要return  上下的要置为null
        if (sortParams.sortField === sortField) {
          return
        }
        sortParams.sortField = sortField
        sortParams.sortMethod = null
      }
      // 触发  sort-change 事件  通知父组件请求数据跟新 并把参数传过去
      emit('sort-change', sortParams)
    }
    const changeCheck = () => {
      emit('sort-change', sortParams)
    }
    return { sortParams, changeSort, changeCheck }
  }
}
</script>

<style lang="less" scoped>
@import '../../../assets/styles/variable.less';
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all .3s;
      &.active {
        background: @c;
        border-color: @c;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
            &.active {
            border-bottom-color: @c;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @c;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
</style>
